package com.ge.executor.enums;

import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

/**
 * 编程语言类型枚举
 */
@Getter
public enum LanguageCmdEnum {

    JAVA("java", "Main.java", new String[]{"javac", "-encoding", "utf-8", "Main.java"}, new String[]{"java", "-Dfile.encoding=UTF-8", "Main"}),
    CPP("cpp", "main.cpp", new String[]{"g++", "-finput-charset=UTF-8", "-fexec-charset=UTF-8", "-o", "main", "main.cpp"}, new String[]{"./main"}),
    C("c", "main.c", new String[]{"gcc", "-finput-charset=UTF-8", "-fexec-charset=UTF-8", "-o", "main", "main.c"}, new String[]{"./main"}),
    PYTHON3("python", "main.py", null, new String[]{"python3", "main.py"}),
    JAVASCRIPT("javascript", "main.js", null, new String[]{"node", "main.js"}),
    TYPESCRIPT("typescript", "main.ts", null, new String[]{"node", "main.ts"}),
    GO("go", "main.go", null, new String[]{"go", "run", "main.go"}),
    ;

    /**
     * 语言
     */
    private final String language;

    /**
     * 存储文件名
     */
    private final String saveFileName;

    /**
     * 语言编译cmd
     * 不需要编译的语言，设置为空
     */
    private final String[] compileCmd;

    /**
     * 语言运行cmd
     */
    private final String[] runCmd;

    LanguageCmdEnum(String language, String saveFileName, String[] compileCmd, String[] runCmd) {
        this.language = language;
        this.saveFileName = saveFileName;
        this.compileCmd = compileCmd;
        this.runCmd = runCmd;
    }

    /**
     * 根据 language 获取枚举
     *
     * @param language 语言类型
     * @return {@link LanguageCmdEnum}
     */
    public static LanguageCmdEnum getEnumByValue(String language) {
        if (StringUtils.isBlank(language)) {
            return null;
        }
        for (LanguageCmdEnum e : LanguageCmdEnum.values()) {
            if (e.getLanguage().equals(language)) {
                return e;
            }
        }
        return null;
    }
}
